﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Text;
using Yipusoft.Account.BLL;
using System.Data;
using Yipusoft.Account.Model;

public partial class BasicBase_DeviceResult : System.Web.UI.Page
{


    //设置每页行数
    private int pageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetCount();
            BindGrid(0);

        }
    }

    private void SetCount()
    {
        int Count = 0;
        string Where = Request.QueryString["Where"];
        if (Where.Length > 0)
        {

            Where = " where " + Where;
        }
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append("select * from devices " + Where + " order by DepartmentID ");
        DataTable dtb = BaseSplitPage.ExecuteSQL(selectCommandText.ToString()).Tables[0];
        foreach (DataRow drw in dtb.Rows)
        {
            Count += (int)drw["DeviceCount"];
        }
        footer.InnerText = "总数量：" + Count;
    }
    void init()
    {

    }

    public string GetCodeItem(string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass);
        return info != null ? info.ItemName : "";
    }

    public string GetType(string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass);
        if (info != null)
        {
            if (info.ItemLevel == 2)
            {
                return CodeItem.GetCodeItem(info.ParentID).ItemName;
            }
            else
            {
                return info.ItemName;
            }
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 绑定GridView
    /// </summary>
    private void BindGrid(int pageIndex)
    {
        string Where = Request.QueryString["Where"];
        if (Where.Length > 0)
        {

            Where = " where " + Where;
        }
        ViewState["PageIndex"] = pageIndex;
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append("select * from devices " + Where + " order by DepartmentID ");
        UserGrid.DataSource = BaseSplitPage.GetPageData(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        UserGrid.DataBind();

        int totalCount = BaseSplitPage.GetRowCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        ViewState["PageCount"] = BaseSplitPage.GetPageCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * pageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(int.Parse(JumpList.SelectedValue));
    }
    protected string GetDepartmentName(string data)
    {
        DepartmentInfo department = Department.GetDepartment(data);
        if (department != null)
        {
            return department.FullName;
        }
        else
        {
            return "单位已删除";
        }
    }
}
